﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class UnityUWBClient : MonoBehaviour
{
    private UWBClient uwbClient;//UWB客户端
    [Header("登录信息组件")]
    public LoginInput loginInput;
    [Header("提示框")]
    public DialogShow dialogShow;

    [Header("是否已经登录")]
    public bool isLogin;

    /// <summary>
    /// 锚点
    /// </summary>
    [Header("锚点信息")]
    public float anchor0X;
    public float anchor0Y;
    public float anchor0Z;
    public float anchor1X;
    public float anchor1Y;
    public float anchor1Z;
    public float anchor2X;
    public float anchor2Y;
    public float anchor2Z;

    [Header("标签信息")]
    /// <summary>
    /// 目标点
    /// </summary>
    public float tagX;
    public float tagY;
    public float tagZ;
    public int tagId;


    [Header("服务器IP地址")]
    public string ip;
    [Header("服务器端口号")]
    public int port;
    [Header("用户账号")]
    public string account;
    [Header("用户密码")]
    public string password;
    [Header("服务器消息")]
    public string serverMsg;


    bool isLoginSuccessd = false;

    /// <summary>
    /// 通过标志位让主线程刷新UI
    /// </summary>
    private bool UpUiFlag = false;
    /// <summary>
    /// 消息
    /// </summary>
    private DialogShowMsg dialogShowMsg;

    /// <summary>
    /// 多线程辅助变量，是否有UI更新
    /// </summary>
    private bool isUpdataUi = false;
    /// <summary>
    /// 界面是否显示，多线程
    /// </summary>
    private bool UiState = false;

    void Start()
    {
        loginInput.Open(!isLoginSuccessd);
    }

    private void UwbClient_ServerMessageEvent(string msg)
    {
        serverMsg = msg;
        Debug.Log("服务器消息：" + msg);
    }

    private void UwbClient_LoginMessageEvent(bool sta)
    {
        isLogin = sta;
        if (sta)
        {
            isUpdataUi = true;
            UiState = false;
            ThShow(account+"\n登录成功！", true);

            isLoginSuccessd = true;
        }
        else
        {
            isUpdataUi = true;
            UiState = true;
            ThShow("登录失败！", true);
        }
    }
    /// <summary>
    /// 更新累计时间
    /// </summary>
    private float UpSumTime = 0;
    void Update()
    {
        if (isLogin)//如果登录
        {
            UpSumTime += Time.deltaTime;//累计时间
            if (UpSumTime > 0.3)//0.3s
            {
                UpSumTime = 0;
                UpTimethread();//更新数据
            }
        }

        if (UpUiFlag)
        {
            UpUiFlag = false;
            dialogShow.Show(dialogShowMsg.dialogStr, dialogShowMsg.isSelfCloes);
        }

        if (isUpdataUi)
        {
            isUpdataUi = false;
            if (UiState)
            {
                loginInput.Open(!isLoginSuccessd);
            }
            else
            {
                loginInput.Close();
            }
        }
    }

    public void ThShow(string txt, bool isSelfClose)
    {
        dialogShowMsg = new DialogShowMsg();
        dialogShowMsg.isSelfCloes = isSelfClose;
        dialogShowMsg.dialogStr = txt;
        UpUiFlag = true;
    }

    private void UpTimethread()
    {
        if (uwbClient != null)
        {
            if (isLogin)
            {
                anchor0X = uwbClient.anchor0X;
                anchor1X = uwbClient.anchor1X;
                anchor2X = uwbClient.anchor2X;
                anchor0Y = uwbClient.anchor0Y;
                anchor1Y = uwbClient.anchor1Y;
                anchor2Y = uwbClient.anchor2Y;
                anchor0Z = uwbClient.anchor0Z;
                anchor1Z = uwbClient.anchor1Z;
                anchor2Z = uwbClient.anchor2Z;

                tagId = uwbClient.tagId;
                tagX = uwbClient.tagX;
                tagY = uwbClient.tagY;
                tagZ = uwbClient.tagZ;
                string str =
                    "锚点1：(" + uwbClient.anchor0X + "," + uwbClient.anchor0Y + "," + uwbClient.anchor0Z + ")\n" +
                    "锚点2：(" + uwbClient.anchor1X + "," + uwbClient.anchor1Y + "," + uwbClient.anchor1Z + ")\n" +
                    "锚点3：(" + uwbClient.anchor2X + "," + uwbClient.anchor2Y + "," + uwbClient.anchor2Z + ")\n" +
                    "标签" + uwbClient.tagId + "：(" + uwbClient.tagX + "," + uwbClient.tagY + "," + uwbClient.tagZ + ")\n" +
                    this.serverMsg;
                Debug.Log(str);
               // if (isDebug) Console.WriteLine(str);
            }
           // if (isDebug) Console.WriteLine("更新界面，没有登录");
        }
       // if (isDebug) Console.WriteLine("更新界面，没对象");
    }

    public void Login()
    {
        Debug.Log("登录");
        if (loginInput!=null)
        {
            //Debug.Log("登录1");
            if (loginInput.GetIp(ref ip))
            {
                //Debug.Log("登录2");
                //成功获取IP
                string p = "";
                if (loginInput.GetPort(ref p))
                {
                    //Debug.Log("登录3");
                    port = int.Parse(p);
                    if(port>0&&port<65535)
                    {
                        //Debug.Log("登录4");
                        //成功获取端口
                        if (loginInput.GetAccount(ref account))
                        {
                            //Debug.Log("登录5");
                            //成功获取账号
                            if (loginInput.GetPwd(ref password))
                            {
                                //成功获取密码
                                Debug.Log("ip:"+ip+"port:"+port+"name:"+account+"pwd:"+password);

                                uwbClient = new UWBClient();
                                uwbClient.LoginMessageEvent += UwbClient_LoginMessageEvent;
                                uwbClient.ServerMessageEvent += UwbClient_ServerMessageEvent;
                                uwbClient.Login(ip, port, account, password);

                                dialogShow.Show("正在登录......", false);

                            }
                            else
                            {
                                dialogShow.Show("密码不正确！",true);
                            }
                        }
                        else
                        {
                            dialogShow.Show("账号不正确！", true);
                        }
                    }
                    else
                    {
                        dialogShow.Show("端口号大小不正确！", true);
                    }
                }
                else
                {
                    dialogShow.Show("端口号不正确！", true);
                }
            }
            else
            {
                dialogShow.Show("IP地址不正确！", true);
            }
        }
        else
        {
            dialogShow.Show("系统错误！", true);
            Debug.LogWarning("loginInput为空！");
        }
    }

    public void LogOut()
    {

    }
}


